package edu.hawaii.senin.iclouds.simple;

import java.util.Map;
import java.util.TreeMap;
import java.util.TreeSet;

import edu.hawaii.senin.iclouds.basics.ReadConfigException;

/**
 * Helper for transferring data from View component to the Model and back.
 * 
 * @author Pavel Senin.
 * 
 */
public class CloudParametersMessage {

  /** Temperature value. */
  public static final String TEMPERATURE = "temperature";
  /** Density value. */
  public static final String DENSITY = "density";
  /** Number of surface active sites. */
  public static final String SITESNUM = "sitesnum";
  /** The grain size. */
  public static final String GRAINSIZE = "grainsize";
  /** D to H ratio value. */
  public static final String DHRATIO = "dhratio";
  /** Bury routine of and off value. */
  public static final String BURY = "bury";
  /** Cascade routine of and off value. */
  public static final String CASCADE = "cascade";
  /** The iterations limit. */
  public static final String ITRLIMIT = "iterationslimit";
  /** The accretion step value. */
  public static final String ACCSTEP = "accretionstep";
  /** The configuration name. */
  public static final String CFGNAME = "configname";
  /** The log file name. */
  public static final String LOGNAME = "logname";
  /** The logger file name. */
  public static final String LOGGERNAME = "loggername";
  /** The logger type, xml or text values are accepted. */
  public static final String LOGGERTYPE = "loggertype";
  /** The logger level. */
  public static final String LOGGERLEVEL = "loggerlevel";

  /** The parameters container. */
  private Map<String, String> parameters = new TreeMap<String, String>();

  /** The legal names container. */
  private TreeSet<String> legalNames;

  /** This array is used within the checking for eligibility routines. */
  private String[] eligible = { TEMPERATURE, DENSITY, SITESNUM, GRAINSIZE, DHRATIO, BURY, CASCADE,
      ITRLIMIT, ACCSTEP, CFGNAME, LOGNAME, LOGGERNAME, LOGGERTYPE, LOGGERLEVEL };

  /**
   * Initializes the message container.
   * 
   */
  public CloudParametersMessage() {
    this.parameters = new TreeMap<String, String>();
    this.legalNames = new TreeSet<String>();
    for (String name : eligible) {
      this.legalNames.add(name);
    }
  }

  /**
   * Adds parameter to the message, if name found wrong, throws exception.
   * 
   * @param parameterName parameter name.
   * @param parameterValue parameterValue.
   * @throws ReadConfigException if wrong parameter name passed.
   */
  public void addParameter(String parameterName, String parameterValue) throws ReadConfigException {
    if (this.legalNames.contains(parameterName)) {
      if (this.parameters.containsKey(parameterName)) {
        this.parameters.remove(parameterName);
      }
      this.parameters.put(parameterName, parameterValue);
    }
    else {
      throw new ReadConfigException("Error while trying to pass parameters " + parameterName
          + " = " + parameterValue);
    }
  }

  /**
   * Reports all parameters.
   * 
   * @return map containing all parameters.
   */
  public Map<String, String> getParameters() {
    return this.parameters;
  }

  /**
   * Reports whether or not such a parameters is within the message.
   * 
   * @param string the parameter name.
   * @return true if exists, false if not.
   */
  public boolean contains(String string) {
    return this.parameters.containsKey(string);
  }

  /**
   * Gets the parameter value.
   * 
   * @param string the name of the parameter querying.
   * @return the parameter value.
   */
  public String get(String string) {
    return this.parameters.get(string);
  }

  /**
   * Prints all parameters within the message.
   * 
   */
  public void print() {
    for (String pn : this.parameters.keySet()) {
      System.out.println(pn + " " + this.parameters.get(pn));
    }
  }

}
